package com.example.yml0528;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.example.yml.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    private TextView title;
    private ViewPager page;
    private RadioGroup group;
    private RadioButton wx;
    private RadioButton txl;
    private RadioButton fx;
    private RadioButton wd;
    private FragmentManager manager;
    private FragmentTransaction ft;
    String[] titles = new String[]{"微信", "通讯录", "发现", "我的"};
    public User user;
    List<Fragment> list;
    private MyPageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        addfra();
    }

    public void init() {
        title = findViewById(R.id.title);
        page = findViewById(R.id.page);
        wx = findViewById(R.id.wx);
        txl = findViewById(R.id.txl);
        fx = findViewById(R.id.fx);
        wd = findViewById(R.id.wd);
        group = findViewById(R.id.group);
        Intent intent = getIntent();
        user = (User) intent.getSerializableExtra("user");
        manager = getSupportFragmentManager();
        group.setOnCheckedChangeListener(this);
    }

    public void addfra() {
        list = new ArrayList<>();
        OneFragment one = new OneFragment();
        TwoFragment two = new TwoFragment();
        ThreeFragment three = new ThreeFragment();
        FourFragment four = new FourFragment();
        list.add(one);
        list.add(two);
        list.add(three);
        list.add(four);
        adapter = new MyPageAdapter(manager, 0, list);
        page.setAdapter(adapter);
        page.setCurrentItem(0);
        page.setOnPageChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.wx:
                page.setCurrentItem(0);
                break;
            case R.id.txl:
                page.setCurrentItem(1);
                break;
            case R.id.fx:
                page.setCurrentItem(2);
                break;
            case R.id.wd:
                page.setCurrentItem(3);
                break;
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        title.setText(titles[position]);

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == 2) {
            switch (page.getCurrentItem()
            ) {
                case 0:
                    wx.setChecked(true);
                    break;
                case 1:
                    txl.setChecked(true);
                    break;
                case 2:
                    fx.setChecked(true);
                    break;
                case 3:
                    wd.setChecked(true);
                    break;
            }
        }
    }
}
